package net.lab1024.sa.admin.module.business.pdf.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.config.PdfConfig;
import net.lab1024.sa.admin.module.business.pdf.util.OcrDiagnosticTool;
import net.lab1024.sa.admin.module.business.pdf.util.OcrTestTool;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.code.SystemErrorCode;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * PDF OCR诊断控制器
 *
 * @Author 1024创新实验室
 */
@Slf4j
@RestController
@RequestMapping("/pdf/ocr-diagnostic")
@Tag(name = "PDF OCR诊断")
public class PdfOcrDiagnosticController {

    @Resource
    private PdfConfig pdfConfig;

    @Resource
    private OcrTestTool ocrTestTool;

    @Resource
    private OcrDiagnosticTool diagnosticTool;

    @GetMapping("/check-config")
    @Operation(summary = "检查OCR配置")
    public ResponseDTO<Map<String, Object>> checkOcrConfig() {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 检查配置
            result.put("tessdataPath", pdfConfig.getTessdataPath());
            result.put("imageDpi", pdfConfig.getOcr().getImageDpi());
            result.put("enableImagePreprocessing", pdfConfig.getOcr().getEnableImagePreprocessing());
            result.put("defaultLanguage", pdfConfig.getOcr().getDefaultLanguage());
            result.put("languages", pdfConfig.getOcr().getLanguages());
            
            // 检查语言包
            Map<String, Object> languagePackCheck = ocrTestTool.checkLanguagePacks(pdfConfig.getTessdataPath());
            result.put("languagePackCheck", languagePackCheck);
            
            // 基本功能测试
            Map<String, Object> basicTest = ocrTestTool.testTesseractBasic(pdfConfig.getTessdataPath());
            result.put("basicTest", basicTest);
            
            return ResponseDTO.ok(result);
            
        } catch (Exception e) {
            log.error("检查OCR配置失败: {}", e.getMessage());
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "检查OCR配置失败: " + e.getMessage());
        }
    }

    @PostMapping("/test-image")
    @Operation(summary = "测试图像OCR识别")
    public ResponseDTO<Map<String, Object>> testImageOcr(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "请选择测试图像文件");
            }
            
            // 保存临时文件
            File tempFile = File.createTempFile("ocr_test_", ".png");
            file.transferTo(tempFile);
            
            try {
                // 测试中文识别
                Map<String, Object> testResult = ocrTestTool.testChineseRecognition(
                    pdfConfig.getTessdataPath(), tempFile);
                
                return ResponseDTO.ok(testResult);
                
            } finally {
                // 删除临时文件
                if (tempFile.exists()) {
                    tempFile.delete();
                }
            }
            
        } catch (Exception e) {
            log.error("测试图像OCR失败: {}", e.getMessage());
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "测试图像OCR失败: " + e.getMessage());
        }
    }

    @GetMapping("/system-info")
    @Operation(summary = "获取系统信息")
    public ResponseDTO<Map<String, Object>> getSystemInfo() {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // JVM信息
            Runtime runtime = Runtime.getRuntime();
            result.put("maxMemory", runtime.maxMemory() / 1024 / 1024 + " MB");
            result.put("totalMemory", runtime.totalMemory() / 1024 / 1024 + " MB");
            result.put("freeMemory", runtime.freeMemory() / 1024 / 1024 + " MB");
            result.put("usedMemory", (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024 + " MB");
            
            // 系统信息
            result.put("javaVersion", System.getProperty("java.version"));
            result.put("osName", System.getProperty("os.name"));
            result.put("osVersion", System.getProperty("os.version"));
            result.put("userDir", System.getProperty("user.dir"));
            
            return ResponseDTO.ok(result);
            
        } catch (Exception e) {
            log.error("获取系统信息失败: {}", e.getMessage());
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "获取系统信息失败: " + e.getMessage());
        }
    }
} 